//
//  GYAddNewAddressViewController.m
//  GoodLuck
//
//  Created by 郭为勇 on 2018/3/3.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYAddNewAddressViewController.h"
#import "GYCitySelectViewController.h"
#import "GYNetWorkTools.h"
#import "GYConfirmOrderViewController.h"

@interface GYAddNewAddressViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nameTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextFiled;
@property (strong, nonatomic) IBOutlet UITextField *addressTextFiled;

@property (weak, nonatomic) IBOutlet UIButton *cityBtu;

@property (weak, nonatomic) IBOutlet UISwitch *switchState;
@property (weak, nonatomic) IBOutlet UIButton *saveBtu;

/** 省级ID */
@property (nonatomic, strong)  NSString *province_id;

/** 市级ID */
@property (nonatomic, strong)  NSString *city_id;

/** 区级ID */
@property (nonatomic, strong)  NSString *area_id;

/** 地址 */
@property (nonatomic, strong) NSString *area_info;


/** 是否默认 */
@property (nonatomic, strong) NSString *is_default;
@end

@implementation GYAddNewAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.is_default = @"0";
    
    self.navigationItem.title = self.isEditState ? @"编辑收货地址" : @"新增收货地址";
    self.saveBtu.layer.cornerRadius = 4;
    
    //如果是要编辑地址 请赋值
    if (self.isEditState) {
        self.nameTextFiled.text = self.model.true_name;
        self.phoneTextFiled.text = self.model.mob_phone;
        self.addressTextFiled.text = self.model.address;
        [self.cityBtu setTitle:self.model.area_info forState:UIControlStateNormal];
        self.switchState.on = self.model.is_default == 0 ? NO : YES;
        self.is_default = [NSString stringWithFormat:@"%d",self.model.is_default];
        
        self.area_id = self.model.area_id;
        self.city_id = self.model.city_id;
        self.area_info = self.model.area_info;
        
    }
}

- (IBAction)defaultValueChanged:(UISwitch *)sender {
    if (sender.isOn) {
       self.is_default = @"1";
    }else {
        self.is_default = @"0";
    }
}


//保存地址按钮点击事件
- (IBAction)saveAddressBtuClick {
    //这里要判断上述填写信息
    if (self.nameTextFiled.text.length == 0) {
        NSLog(@"请填写完整的收货信息...");
        return ;
    }
    
    if (![self isMobileNumber:self.phoneTextFiled.text]) {
        NSLog(@"请填写完整的收货信息...");
        return ;
    }
    
    NSString *title = self.cityBtu.currentTitle;
    if (title.length == 0) {
        NSLog(@"请填写完整的收货信息...");
        return ;
    }
    
    if (self.addressTextFiled.text.length == 0) {
        NSLog(@"请填写完整的收货信息...");
        return ;
    }
    
    //实现网络保存地址的接口  然后返回查看地址
    if (self.isEditState) {
        //发送修改地址的指令
        [GYNetWorkTools requestUpdateUserAddress:self.model.address_id true_name:self.nameTextFiled.text mob_phone:self.phoneTextFiled.text city_id:self.city_id area_id:self.area_id address:self.addressTextFiled.text area_info:self.area_info is_default:self.is_default success:^(NSDictionary *obj) {
            int code = [[obj objectForKey:@"code"] intValue];
            if (code == 200) {
                //[EasyTextView showSuccessText:@"修改地址成功!"];
                [self.navigationController popViewControllerAnimated:YES];
            }else if (code == 400) {
                [EasyTextView showText:@"修改地址失败!"];
            }
        } error:^(NSError *error) {
            NSLog(@"UpdateUserAddressFailed====%@",error);
        }];
    }else {
        //发送增加地址的指令
        [GYNetWorkTools requestAddUserAddress:self.nameTextFiled.text mob_phone:self.phoneTextFiled.text city_id:self.city_id area_id:self.area_id address:self.addressTextFiled.text area_info:title is_default:self.is_default success:^(NSDictionary *obj) {
            //NSLog(@"obj ======= %@",obj);
            int code = [[obj objectForKey:@"code"] intValue];
            if (code == 200) {
                //[EasyTextView showSuccessText:@"增加收货地址成功!"];
                //判断是否是订单直接增加地址
//                if (self.isOrderAddress) {
//                    for (UIViewController *viewController in self.navigationController.viewControllers) {
//                        if ([viewController isMemberOfClass:[GYConfirmOrderViewController class]]) {
//                            [self.navigationController popToViewController:viewController animated:YES];
//                        }
//                    }
//                }else {
                    [self.navigationController popViewControllerAnimated:YES];
//                }
            }
            
        } error:^(NSError *error) {
            [EasyTextView showText:@"添加地址失败!"];
            NSLog(@"AddUserAddressFailed====%@",error);
        }];
    }
}

//选择地区按钮
- (IBAction)selectCity:(UIButton *)sender {
    
    GYCitySelectViewController *citySelectVC = [[GYCitySelectViewController alloc] initWithNibName:@"GYCitySelectViewController" bundle:nil];
    __weak typeof(self) weakSelf = self;
    citySelectVC.block = ^(NSString *addressName, NSString *province_id, NSString *city_id, NSString *area_id) {
        [sender setTitle:addressName forState:UIControlStateNormal];
        weakSelf.province_id = province_id;
        weakSelf.city_id = city_id;
        weakSelf.area_id = area_id;
        weakSelf.area_info = addressName;
    };
    [self.navigationController pushViewController:citySelectVC animated:YES];
}


#pragma mark--判断手机号码
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    //    电信号段:133/153/180/181/189/177
    //    联通号段:130/131/132/155/156/185/186/145/176
    //    移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
    //    虚拟运营商:170
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    //     NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    NSString * CM = @" ^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    //     NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    NSString * CU =  @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    //     NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    NSString * CT = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}


@end
